﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SharpDX.DirectInput;
using TableTool.Model;
using TlToolBase;

namespace TableTool
{
    public partial class PRHelp : Form
    {
        /// <summary>
        /// 流逝 8s
        /// </summary>
        AlarmFrm alarmFrm12 { get => alarmFrms.Find(x => x.Name == nameof(alarmFrm12)); }

        /// <summary>
        /// 魔攻
        /// </summary>
        AlarmFrm alarmFrm23 { get => alarmFrms.Find(x => x.Name == nameof(alarmFrm23)); }
        /// <summary>
        /// 防御
        /// </summary>
        AlarmFrm alarmFrm32 { get => alarmFrms.Find(x => x.Name == nameof(alarmFrm32)); }
        /// <summary>
        /// 破防
        /// </summary>
        AlarmFrm alarmFrm11 { get => alarmFrms.Find(x => x.Name == nameof(alarmFrm11)); }
        /// <summary>
        /// 王减
        /// </summary>
        AlarmFrm alarmFrm22 { get => alarmFrms.Find(x => x.Name == nameof(alarmFrm22)); }
        /// <summary>
        /// 攻击+防御
        /// </summary>
        AlarmFrm alarmFrm33 { get => alarmFrms.Find(x => x.Name == nameof(alarmFrm33)); }

        List<AlarmFrm> alarmFrms { get; set; }

        private bool saveFalg1 { get; set; }
        private bool saveFalg2 { get; set; }
        private bool saveFalg3 { get; set; }
        private bool saveFalg4 { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public PRHelpConfig model { get; set; }
        public PRHelp()
        {
            InitializeComponent();
            this.Load += PRHelp_Load;
            this.FormClosing += PRHelp_FormClosing;
        }

        private void PRHelp_FormClosing(object? sender, FormClosingEventArgs e)
        {
            timer1.Stop();
            foreach (AlarmFrm alarmFrm in alarmFrms)
            {
                alarmFrm.Close();
            }
            FileDataClass.Save(model);
        }
        string[] armList = new string[]
        {
            nameof(alarmFrm33),
            nameof(alarmFrm11),
            nameof(alarmFrm12),
            nameof(alarmFrm23),
            nameof(alarmFrm22),
            nameof(alarmFrm32),
        };
        private void PRHelp_Load(object? sender, EventArgs e)
        {
            model = FileDataClass.Get<PRHelpConfig>();
            alarmFrms = new List<AlarmFrm>();
            for (int i = 0; i < armList.Length; i++)
            {
                var alarm = new AlarmFrm();
                alarm.Name = armList[i];
                alarmFrms.Add(alarm);
            }
            alarmFrm12.lookModel = new LookModel()
            {
                Name = "流逝12",
                timeSecond = 8
            };
            alarmFrm23.lookModel = new LookModel()
            {
                Name = "魔攻23",
                timeSecond = 30
            };
            alarmFrm32.lookModel = new LookModel()
            {
                Name = "防御32",
                timeSecond = 30,
            };
            alarmFrm11.lookModel = new LookModel()
            {
                Name = "破防11",
                timeSecond = 30,
            };
            alarmFrm22.lookModel = new LookModel()
            {
                Name = "王减22",
                timeSecond = 30,
            };
            alarmFrm33.lookModel = new LookModel()
            {
                Name = "攻击盾33",
                timeSecond = 30,
            };
            SetLable();
            textBox1.Text = model.frmSize;
            timer1.Start();
        }

        private void PRHelp_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (saveFalg1)
            {
                saveFalg1 = false;
            }
            else
            {
                if (model.P1 == null)
                {
                    model.P1 = new();
                }
                model.P1.Clear();
                SetLable();
                saveFalg1 = true;
            }
            btnP1.Text = saveFalg1 ? "停止记录" : "记录P1";
        }
        private void SetLable()
        {
            if (IsHandleCreated)
            {
                if (this.InvokeRequired)
                {
                    this.Invoke(new Action(SetLable));
                }
                else
                {
                    SetShowLable(label1, model.P1);
                    SetShowLable(label2, model.P2);
                    SetShowLable(label3, model.P3);
                    SetShowLable(label5, model.P4);
                }
            }
        }
        private void SetShowLable(Label label, List<Key> keys)
        {
            label.Text = "";
            if (keys != null)
            {
                for (int i = 0; i < keys.Count; i++)
                {
                    if (i > 0)
                    {
                        label.Text += ",";
                    }
                    label.Text += keys[i].ToString();
                }
            }
        }
        private void button1_Click_1(object sender, EventArgs e)
        {
            if (saveFalg4)
            {
                saveFalg4 = false;
            }
            else
            {
                if (model.P4 == null)
                {
                    model.P4 = new();
                }
                model.P4.Clear();
                SetLable();
                saveFalg4 = true;
            }
            button1.Text = saveFalg4 ? "停止记录" : "记录刷新";
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (saveFalg2)
            {
                saveFalg2 = false;
            }
            else
            {
                if (model.P2 == null)
                {
                    model.P2 = new();
                }
                model.P2.Clear();
                SetLable();
                saveFalg2 = true;
            }
            button2.Text = saveFalg2 ? "停止记录" : "记录P2";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (saveFalg3)
            {
                saveFalg3 = false;
            }
            else
            {
                if (model.P3 == null)
                {
                    model.P3 = new();
                }
                model.P3.Clear();
                SetLable();
                saveFalg3 = true;
            }
            button3.Text = saveFalg3 ? "停止记录" : "记录P3";
        }

        private void checkBox1_Click(object sender, EventArgs e)
        {
            this.TopMost = checkBox1.Checked;
        }

        private void checkBox2_Click(object sender, EventArgs e)
        {
            int height = 100;
            int width = 200;
            var size = textBox1.Text.Split(',');
            if (size.Length == 2)
            {
                int.TryParse(size[0].ToString(), out height);
                int.TryParse(size[1].ToString(), out width);
            }
            else
            {
                textBox1.Text = "100,200";
            }
            model.frmSize = textBox1.Text;
            var type = typeof(PRHelpConfig);
            if (checkBox2.Checked)
            {
                foreach (var item in alarmFrms)
                {
                    var pro = type.GetProperty(item.Name);
                    item.IsShow = true;
                    item.Show();
                    item.TopMost = true;
                    if (pro != null)
                    {
                        var location = pro.GetValue(model);
                        if (location != null)
                        {
                            Point r = (Point)location;
                            item.Width = width;
                            item.Height = height;
                            item.Location = new Point(r.X, r.Y);
                        }
                    }
                }
            }
            else
            {
                foreach (var item in alarmFrms)
                {
                    var pro = type.GetProperty(item.Name);
                    if (pro != null)
                    {
                        pro.SetValue(model, item.Location);
                    }
                    item.IsShow = false;
                    item.Hide();

                }
            }
        }

        private void ShowHisInput()
        {
            if (label4.IsHandleCreated)
            {
                if (label4.InvokeRequired)
                {
                    label4.Invoke(new Action(ShowHisInput));
                }
                else
                {
                    while (input.Count > 5)
                    {
                        input.RemoveAt(0);
                    }
                    string showtx = $"L{Last} I{DownKey}  ";
                    foreach (var item in input)
                    {
                        showtx += $"{item},";
                    }
                    label4.Text = showtx;
                }
            }

        }
        List<Key> input = new List<Key>();
        Key Last = Key.Escape;
        Key DownKey = Key.Escape;
        DateTime LastInputTime = DateTime.Now;


        /// <summary>
        ///
        /// </summary>
        public void KeyInput(Key keyinput)
        {
            if (keyinput == DownKey && DateTime.Now - LastInputTime <= TimeSpan.FromMilliseconds(1000))
            {
                return;
            }
            else
            {
                LastInputTime = DateTime.Now;
            }
            DownKey = keyinput;
            input.Add(keyinput);

            bool addFlag = false;
            if (saveFalg1)
            {
                model.P1.Add(keyinput);
                addFlag = true;
            }
            if (saveFalg2)
            {
                model.P2.Add(keyinput); addFlag = true;
            }
            if (saveFalg3)
            {
                model.P3.Add(keyinput); addFlag = true;
            }
            if (saveFalg4)
            {
                model.P4.Add(keyinput); addFlag = true;
            }
            if (addFlag)
            {
                SetLable();
                return;
            }
            if (model.P1 == null || model.P2 == null || model.P3 == null)
            {
                return;
            }
            if (model.P4 != null)
            {
                if (model.P4.Contains(keyinput))
                {
                    foreach (var item in alarmFrms)
                    {
                        if (item.IsOk == false)
                        {
                            item.LastTime = DateTime.Now;
                        }
                    }
                }
            }
            int index1 = 0;
            int index2 = 0;
            if (model.P1.Contains(Last))
            {
                index1 = 1;
            }
            if (model.P2.Contains(Last))
            {
                index1 = 2;
            }
            if (model.P3.Contains(Last))
            {
                index1 = 3;
            }
            if (model.P1.Contains(keyinput))
            {
                index2 = 1;
            }
            if (model.P2.Contains(keyinput))
            {
                index2 = 2;
            }
            if (model.P3.Contains(keyinput))
            {
                index2 = 3;
            }
            if (index1 == 0 && index2 != 0)
            {
                Last = keyinput;
                ShowHisInput();
                return;
            }
            if (index1 != 0 && index2 != 0)
            {
                //if (Last == keyinput)
                //{
                //    ShowHisInput();
                //    return;
                //}
                if (index1 == 1)
                {
                    if (index2 == 1)
                    {
                        alarmFrm11.LastTime = DateTime.Now;
                    }
                    if (index2 == 2)
                    {
                        alarmFrm12.LastTime = DateTime.Now;
                    }
                }
                if (index1 == 2)
                {
                    if (index2 == 2)
                    {
                        alarmFrm22.LastTime = DateTime.Now;
                    }
                    if (index2 == 3)
                    {
                        alarmFrm23.LastTime = DateTime.Now;
                    }
                }
                if (index1 == 3)
                {
                    if (index2 == 2)
                    {
                        alarmFrm32.LastTime = DateTime.Now;
                    }
                    if (index2 == 3)
                    {
                        alarmFrm33.LastTime = DateTime.Now;
                    }
                }
                Last = Key.Escape;
            }
            ShowHisInput();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            foreach (var item in alarmFrms)
            {
                item.Ex();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            model.alarmFrm11 = new Point(0,0);
            model.alarmFrm12 = new Point(0,0);
            model.alarmFrm33 = new Point(0,0);
            model.alarmFrm32 = new Point(0,0);
            model.alarmFrm23 = new Point(0,0);
            model.alarmFrm22 = new Point(0,0);
        }
    }
}
